package com.dk.mydemo.interceptor;

import com.dk.mydemo.base.ResponseEntity;
import com.dk.mydemo.exception.BusinessException;
import com.dk.mydemo.exception.BusinessExceptionFactory;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/** 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author dk 
* @date 2015年9月17日 下午7:16:07 
*  
*/
@ControllerAdvice
public class GlobalExceptionHandler {
	protected Logger logger = Logger.getLogger(getClass());
	@Autowired
	private BusinessExceptionFactory exceptionFactory;

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResponseEntity handle(Exception ex) {
		logger.error("", ex);
		if(ex instanceof BusinessException) {
			String errorCode = ((BusinessException) ex).getErrorCode();
			return ResponseEntity.response(errorCode, ((BusinessException) ex).getMessage());
		} else if(ex instanceof HttpMessageNotReadableException) {//2015/8/19 add 数据转换异常
			return ResponseEntity.response("2","数据格式错误");
		} else {
			return ResponseEntity.response("1","服务访问失败，请稍后重试");
		}
		
	}
}
